package com.vito.oa.email.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.vito.base.model.DataGrid;
import com.vito.oa.email.contact.dao.ExternalContactsGroupMapper;
import com.vito.oa.email.contact.dao.ExternalContactsMapper;
import com.vito.oa.email.contact.dao.OaBaseUserMapper;
import com.vito.oa.email.contact.dao.OaJamesUserMapper;
import com.vito.oa.email.model.ExternalContacts;
import com.vito.oa.email.service.ExternalContactsService;
@Service
public class ExternalContactsServiceImp implements ExternalContactsService{

	@Autowired 
	ExternalContactsGroupMapper groupMapper;
	@Autowired 
	ExternalContactsMapper contactsMapper;
	@Autowired
	private OaJamesUserMapper oaJamesUserMapper;
	
	@Autowired
	private OaBaseUserMapper oaBaseUserMapper;

	public List<ExternalContacts> getContactsByGroupId(String groupId) {
		return contactsMapper.getContactsByGroupId(groupId);
	}

	public boolean addContacts(ExternalContacts contacts) {
		return contactsMapper.addContacts(contacts) > 0;
	}

	public boolean updateContacts(ExternalContacts contacts) {
		return contactsMapper.updateContacts(contacts) > 0;
	}

	public DataGrid<ExternalContacts> getContactsByPage(ExternalContacts contacts) {
		DataGrid<ExternalContacts> data = new DataGrid<ExternalContacts>();
//		int count = contactsMapper.getContactsCount(contacts);
//		data.setTotal(count);
//		if (count > 0) {
			data.setRows(contactsMapper.getContactsByPage(contacts));
			data.setTotal(contacts.getTotal());
//		}
		return data;
	}

	public boolean delContacts(String contactsIds, int status) {
		String contactsId[] = contactsIds.split(",");
		boolean b = false;
		ExternalContacts contacts = new ExternalContacts();
		contacts.setStatus(status);
		if (contactsId.length > 0) {
			for (int i = 0; i < contactsId.length; i++) {
				contacts.setContactsId(contactsId[i]);
				b = contactsMapper.updateContacts(contacts) >0 ;
			}
		}
		return b;
	}

	public ExternalContacts getContactsByMail(ExternalContacts contacts) {
		return contactsMapper.getContactsByMail(contacts);
	}

	public boolean checkUserMailRepeat(String eamil) {
		boolean b = false;
		if (oaJamesUserMapper.getOaJamesUserByUserName(eamil)== 0 && oaBaseUserMapper.getUserMail(eamil) == 0) {
			b = true;
		}
		return b;
	}

	public List<ExternalContacts> getEmailContactsList(String userId) {
		return contactsMapper.getEmailContactsList(userId);
	}

}
